<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

  <title>Class: ResourceUsage</title>

  <link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />

  <script src="./js/jquery.js" type="text/javascript" charset="utf-8"></script>
  <script src="./js/thickbox-compressed.js" type="text/javascript" charset="utf-8"></script>
  <script src="./js/quicksearch.js" type="text/javascript" charset="utf-8"></script>
  <script src="./js/darkfish.js" type="text/javascript" charset="utf-8"></script>

</head>
<body id="top" class="class">

  <div id="metadata">
    <div id="home-metadata">
      <div id="home-section" class="section">
        <h3 class="section-header">
          <a href="./index.html">Home</a>
          <a href="./index.html#classes">Classes</a>
          <a href="./index.html#methods">Methods</a>
        </h3>
      </div>
    </div>

    <div id="file-metadata">
      <div id="file-list-section" class="section">
        <h3 class="section-header">In Files</h3>
        <div class="section-body">
          <ul>
          
            <li><a href="./app/models/resource_usage_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
              class="thickbox" title="app/models/resource_usage.rb">app/models/resource_usage.rb</a></li>
          
          </ul>
        </div>
      </div>

      
    </div>

    <div id="class-metadata">
      
      <!-- Parent Class -->
      <div id="parent-class-section" class="section">
        <h3 class="section-header">Parent</h3>
        
        <p class="link">ActiveRecord::Base</p>
        
      </div>
      

      

      

      
      <!-- Method Quickref -->
      <div id="method-list-section" class="section">
        <h3 class="section-header">Methods</h3>
        <ul class="link-list">
          
          <li><a href="#method-c-fill_gaps-21">::fill_gaps!</a></li>
          
          <li><a href="#method-c-get_application_data">::get_application_data</a></li>
          
          <li><a href="#method-c-get_latest_for_apps">::get_latest_for_apps</a></li>
          
          <li><a href="#method-c-get_latest_for_apps_with_exceptions">::get_latest_for_apps_with_exceptions</a></li>
          
          <li><a href="#method-c-get_latest_for_server">::get_latest_for_server</a></li>
          
          <li><a href="#method-c-get_max_and_slab">::get_max_and_slab</a></li>
          
          <li><a href="#method-c-get_server_resource_usage">::get_server_resource_usage</a></li>
          
        </ul>
      </div>
      

      
    </div>

    <div id="project-metadata">
      
      
      <div id="fileindex-section" class="section project-section">
        <h3 class="section-header">Files</h3>
        <ul>
        
          <li class="file"><a href="./doc/README_FOR_APP.html">README_FOR_APP</a></li>
        
        </ul>
      </div>
      

      <div id="classindex-section" class="section project-section">
        <h3 class="section-header">Class/Module Index
          <span class="search-toggle"><img src="./images/find.png"
            height="16" width="16" alt="[+]"
            title="show/hide quicksearch" /></span></h3>
        <form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
        <fieldset>
          <legend>Quicksearch</legend>
          <input type="text" name="quicksearch" value=""
            class="quicksearch-field" />
        </fieldset>
        </form>

        <ul class="link-list">
        
          <li><a href="./Analytics.html">Analytics</a></li>
        
          <li><a href="./Analytics/Database.html">Analytics::Database</a></li>
        
          <li><a href="./Analytics/ResourceUsage.html">Analytics::ResourceUsage</a></li>
        
          <li><a href="./Analytics/Throughput.html">Analytics::Throughput</a></li>
        
          <li><a href="./Analytics/Url.html">Analytics::Url</a></li>
        
          <li><a href="./Analytics/UrlBreakup.html">Analytics::UrlBreakup</a></li>
        
          <li><a href="./ActionView.html">ActionView</a></li>
        
          <li><a href="./ActionView/Helpers.html">ActionView::Helpers</a></li>
        
          <li><a href="./ActionView/Helpers/AssetTagHelper.html">ActionView::Helpers::AssetTagHelper</a></li>
        
          <li><a href="./AdminController.html">AdminController</a></li>
        
          <li><a href="./AdminHelper.html">AdminHelper</a></li>
        
          <li><a href="./App.html">App</a></li>
        
          <li><a href="./AppException.html">AppException</a></li>
        
          <li><a href="./AppTimeSample.html">AppTimeSample</a></li>
        
          <li><a href="./ApplicationController.html">ApplicationController</a></li>
        
          <li><a href="./ApplicationHelper.html">ApplicationHelper</a></li>
        
          <li><a href="./ApplicationSpecification.html">ApplicationSpecification</a></li>
        
          <li><a href="./ApplicationSpecificationController.html">ApplicationSpecificationController</a></li>
        
          <li><a href="./ApplicationSpecificationHelper.html">ApplicationSpecificationHelper</a></li>
        
          <li><a href="./Control.html">Control</a></li>
        
          <li><a href="./ExceptionDetail.html">ExceptionDetail</a></li>
        
          <li><a href="./ExceptionsController.html">ExceptionsController</a></li>
        
          <li><a href="./ExceptionsHelper.html">ExceptionsHelper</a></li>
        
          <li><a href="./GraphController.html">GraphController</a></li>
        
          <li><a href="./GraphHelper.html">GraphHelper</a></li>
        
          <li><a href="./Headers.html">Headers</a></li>
        
          <li><a href="./HeadersController.html">HeadersController</a></li>
        
          <li><a href="./HeadersHelper.html">HeadersHelper</a></li>
        
          <li><a href="./MailSpecification.html">MailSpecification</a></li>
        
          <li><a href="./MailSpecificationController.html">MailSpecificationController</a></li>
        
          <li><a href="./MailSpecificationHelper.html">MailSpecificationHelper</a></li>
        
          <li><a href="./Mailer.html">Mailer</a></li>
        
          <li><a href="./Object.html">Object</a></li>
        
          <li><a href="./PseudoModel.html">PseudoModel</a></li>
        
          <li><a href="./ResourceUsage.html">ResourceUsage</a></li>
        
          <li><a href="./SCGI.html">SCGI</a></li>
        
          <li><a href="./ServerSpecification.html">ServerSpecification</a></li>
        
          <li><a href="./ServerSpecificationController.html">ServerSpecificationController</a></li>
        
          <li><a href="./ServerSpecificationHelper.html">ServerSpecificationHelper</a></li>
        
          <li><a href="./SignalHelper.html">SignalHelper</a></li>
        
          <li><a href="./UrlBreakupTimeSample.html">UrlBreakupTimeSample</a></li>
        
          <li><a href="./UrlTimeSample.html">UrlTimeSample</a></li>
        
          <li><a href="./User.html">User</a></li>
        
          <li><a href="./YAMLConfig.html">YAMLConfig</a></li>
        
          <li><a href="./YAMLWriter.html">YAMLWriter</a></li>
        
        </ul>
        <div id="no-class-search-results" style="display: none;">No matching classes.</div>
      </div>

      
    </div>
  </div>

  <div id="documentation">
    <h1 class="class">ResourceUsage</h1>

    <div id="description" class="description">
      
<p>This is the model class <a href="ResourceUsage.html">ResourceUsage</a>
related to the resource_usages table in the database.</p>

    </div><!-- description -->

    
    
    
    <div id="5Buntitled-5D" class="documentation-section">
      

      

      

      

      <!-- Methods -->
      
      <div id="public-class-method-details" class="method-section section">
        <h3 class="section-header">Public Class Methods</h3>

      
        <div id="fill_gaps-21-method" class="method-detail ">
          <a name="method-c-fill_gaps-21"></a>

          
          <div class="method-heading">
            <span class="method-name">fill_gaps!</span><span
              class="method-args">(array)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>Due to technical reasons, e.g. sleep(60) , thread can remain in sleep state
for more than 60 seconds, in this case it may not get sample for that
minute. With this method filling any such gaps(consecutive one gap at max)
by predicting usage from around values. Parameters : array(Array) of
integer or float Find out the elements having 0 or 0.0 as value, normalize
it value according to previous/next values.</p>
            

            
            <div class="method-source-code" id="fill_gaps-21-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 195</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">fill_gaps!</span>(<span class="ruby-identifier">array</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>  <span class="ruby-comment"># proceed only if array is not empty</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-comment"># if first element is zero, predict value from next two values.</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">2</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-value">2</span>].<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">array</span>[<span class="ruby-value">0</span>] = (<span class="ruby-identifier">array</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">+</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">2</span>]) <span class="ruby-operator">/</span> <span class="ruby-value">2</span>
      <span class="ruby-keyword">end</span> <span class="ruby-comment"># if array.length &gt; 2</span>
    <span class="ruby-keyword">end</span> <span class="ruby-comment"># if array[0].to_i == 0</span>

    <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">-1</span>] <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-comment">#if last element is zero, predict value from previous two values.</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">-2</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">-3</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-value">-2</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-value">-3</span>].<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">array</span>[<span class="ruby-value">-1</span>] = (<span class="ruby-identifier">array</span>[<span class="ruby-value">-2</span>] <span class="ruby-operator">+</span> <span class="ruby-identifier">array</span>[<span class="ruby-value">-3</span>]) <span class="ruby-operator">/</span> <span class="ruby-value">2</span>
      <span class="ruby-keyword">end</span> <span class="ruby-comment"># if array.length &gt; 2</span>
    <span class="ruby-keyword">end</span> <span class="ruby-comment"># if array[-1].to_i == 0</span>

    <span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">array</span>.<span class="ruby-identifier">length</span><span class="ruby-operator">-</span><span class="ruby-value">1</span> <span class="ruby-comment"># checking for interminant values</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] <span class="ruby-operator">!=</span> <span class="ruby-value">0</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>].<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span>] = (<span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>] <span class="ruby-operator">+</span> <span class="ruby-identifier">array</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>]) <span class="ruby-operator">/</span> <span class="ruby-value">2</span>
      <span class="ruby-keyword">end</span> <span class="ruby-comment"># if</span>
    <span class="ruby-keyword">end</span> <span class="ruby-comment"># for</span>
  <span class="ruby-keyword">end</span> <span class="ruby-comment"># if array.length &gt; 0</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- fill_gaps-21-source -->
            
          </div>

          

          
        </div><!-- fill_gaps-21-method -->

      
        <div id="get_application_data-method" class="method-detail ">
          <a name="method-c-get_application_data"></a>

          
          <div class="method-heading">
            <span class="method-name">get_application_data</span><span
              class="method-args">(app_id, start_time, end_time, type)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>This method Returns the Resource Usage data for a particular application.
This data is used to plot the graph for cpu usage and memory usage for that
application. CPU usage is in percentage and the physical memory usage is
MBs.</p>
            

            
            <div class="method-source-code" id="get_application_data-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 132</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_application_data</span>(<span class="ruby-identifier">app_id</span>, <span class="ruby-identifier">start_time</span>, <span class="ruby-identifier">end_time</span>, <span class="ruby-identifier">type</span>)
  <span class="ruby-identifier">max</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">interval</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">interval</span> = ((<span class="ruby-identifier">end_time</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">start_time</span>) <span class="ruby-operator">/</span> <span class="ruby-value">60</span>).<span class="ruby-identifier">to_i</span>
  <span class="ruby-identifier">final_data</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">interval</span>)
  <span class="ruby-identifier">wall_time</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">interval</span>)
  <span class="ruby-identifier">resource_usages</span> = <span class="ruby-identifier">select</span>(<span class="ruby-string">'sum(cpu_usage) as cpu_usage,
                            sum(memory_usage) as memory_usage,
                            count(*) as count,
                            wall_time'</span>
                            ).<span class="ruby-identifier">where</span>(<span class="ruby-string">'app_id = ? and
                            wall_time &gt;= ? and wall_time &lt; ?'</span>,
                            <span class="ruby-identifier">app_id</span>,
                            <span class="ruby-identifier">start_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>), <span class="ruby-identifier">end_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)
                            ).<span class="ruby-identifier">group</span>(<span class="ruby-value">:wall_time</span>)
  <span class="ruby-identifier">resource_usages</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">resource_usage</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">current_time</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">local</span>(<span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">year</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">month</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">day</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">hour</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">min</span>, <span class="ruby-string">'0'</span>)
    <span class="ruby-identifier">index</span> = (<span class="ruby-identifier">current_time</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">start_time</span>) <span class="ruby-operator">/</span> <span class="ruby-value">60</span>
    <span class="ruby-identifier">count</span> = <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">count</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;cpu&quot;</span>
      <span class="ruby-identifier">total_data</span> = <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">cpu_usage</span>
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;memory&quot;</span>
      <span class="ruby-identifier">total_data</span> = <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">memory_usage</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">1024</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">total_data</span>
      <span class="ruby-identifier">max</span> = <span class="ruby-identifier">total_data</span>.<span class="ruby-identifier">to_i</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">final_data</span>[<span class="ruby-identifier">index</span>] = <span class="ruby-identifier">total_data</span>
    <span class="ruby-identifier">wall_time</span>[<span class="ruby-identifier">index</span>] = <span class="ruby-identifier">current_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%H:%M&quot;</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">interval</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">final_data</span>[<span class="ruby-identifier">i</span>].<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">wall_time</span>[<span class="ruby-identifier">i</span>] = (<span class="ruby-identifier">start_time</span><span class="ruby-operator">+</span><span class="ruby-identifier">i</span>*<span class="ruby-value">60</span>).<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%H:%M&quot;</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">max</span>,<span class="ruby-identifier">slab</span> = <span class="ruby-identifier">get_max_and_slab</span>(<span class="ruby-identifier">max</span>)
  <span class="ruby-identifier">fill_gaps!</span>(<span class="ruby-identifier">final_data</span>)
  <span class="ruby-identifier">step</span> = (<span class="ruby-identifier">interval</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>) <span class="ruby-operator">/</span> <span class="ruby-value">20</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">wall_time</span>, <span class="ruby-identifier">final_data</span>, <span class="ruby-identifier">max</span>, <span class="ruby-identifier">slab</span>, <span class="ruby-identifier">step</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_application_data-source -->
            
          </div>

          

          
        </div><!-- get_application_data-method -->

      
        <div id="get_latest_for_apps-method" class="method-detail ">
          <a name="method-c-get_latest_for_apps"></a>

          
          <div class="method-heading">
            <span class="method-name">get_latest_for_apps</span><span
              class="method-args">()</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>This method is used to get the latest state of the application i.e. its cpu
usage and physical memory usage.</p>

<pre>Returns hash with application name as key and 2 element array as value. In 2 element, first element is cpu usage, 2nd is memory usage in kb</pre>
            

            
            <div class="method-source-code" id="get_latest_for_apps-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 46</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_latest_for_apps</span>

  <span class="ruby-identifier">apps</span> = <span class="ruby-constant">App</span>.<span class="ruby-identifier">select</span>(<span class="ruby-string">'id, name'</span>)

  <span class="ruby-identifier">app_hash</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-identifier">result_hash</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>

  <span class="ruby-identifier">apps</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">app</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">app_hash</span>[<span class="ruby-identifier">app</span>.<span class="ruby-identifier">id</span>] = <span class="ruby-identifier">app</span>.<span class="ruby-identifier">name</span>
    <span class="ruby-identifier">result_hash</span>[<span class="ruby-identifier">app</span>.<span class="ruby-identifier">name</span>] = [<span class="ruby-value">0.0</span>, <span class="ruby-value">0.0</span>]
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">t1</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
  <span class="ruby-identifier">interval</span> = <span class="ruby-value">60</span> <span class="ruby-comment"># in seconds</span>
  <span class="ruby-identifier">t2</span> = <span class="ruby-identifier">t1</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">interval</span>
  <span class="ruby-identifier">result_set</span> = <span class="ruby-identifier">select</span>(<span class="ruby-string">'app_id,
                       sum(cpu_usage) as tot_cpu,
                       sum(memory_usage) as tot_memory'</span>
                       ).<span class="ruby-identifier">where</span>(<span class="ruby-string">'wall_time &lt;= ? and wall_time &gt;= ?'</span>,
                        <span class="ruby-identifier">t1</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>),<span class="ruby-identifier">t2</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)
                        ).<span class="ruby-identifier">group</span>(<span class="ruby-value">:app_id</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">result_set</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
    <span class="ruby-identifier">result_set</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">result</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">result_hash</span>[<span class="ruby-identifier">app_hash</span>[<span class="ruby-identifier">result</span>.<span class="ruby-identifier">app_id</span>]] = [<span class="ruby-identifier">result</span>.<span class="ruby-identifier">tot_cpu</span>.<span class="ruby-identifier">to_f</span>, <span class="ruby-identifier">result</span>.<span class="ruby-identifier">tot_memory</span>.<span class="ruby-identifier">to_i</span>]
    <span class="ruby-keyword">end</span> <span class="ruby-comment"># do |result|</span>
  <span class="ruby-keyword">end</span> <span class="ruby-comment"># if</span>
  <span class="ruby-identifier">result_hash</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_latest_for_apps-source -->
            
          </div>

          

          
        </div><!-- get_latest_for_apps-method -->

      
        <div id="get_latest_for_apps_with_exceptions-method" class="method-detail ">
          <a name="method-c-get_latest_for_apps_with_exceptions"></a>

          
          <div class="method-heading">
            <span class="method-name">get_latest_for_apps_with_exceptions</span><span
              class="method-args">()</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            
            

            
            <div class="method-source-code" id="get_latest_for_apps_with_exceptions-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 76</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_latest_for_apps_with_exceptions</span>
  <span class="ruby-identifier">latest_apps</span> = <span class="ruby-identifier">get_latest_for_apps</span>
  <span class="ruby-identifier">latest_apps</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">exception_count</span> = <span class="ruby-constant">App</span>.<span class="ruby-identifier">exceptions_count</span>(<span class="ruby-identifier">key</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">exception_count</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
      <span class="ruby-identifier">latest_apps</span>[<span class="ruby-identifier">key</span>][<span class="ruby-value">2</span>] = <span class="ruby-node">&quot;&lt;a href=\&quot;exceptions/get_exceptions_list?application_name=#{key}\&quot;&gt;Yes (#{exception_count})&lt;/a&gt;&quot;</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">latest_apps</span>[<span class="ruby-identifier">key</span>][<span class="ruby-value">2</span>] = <span class="ruby-string">&quot;No&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">latest_apps</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_latest_for_apps_with_exceptions-source -->
            
          </div>

          

          
        </div><!-- get_latest_for_apps_with_exceptions-method -->

      
        <div id="get_latest_for_server-method" class="method-detail ">
          <a name="method-c-get_latest_for_server"></a>

          
          <div class="method-heading">
            <span class="method-name">get_latest_for_server</span><span
              class="method-args">()</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>This method is used to get the latest state of the server i.e. its cpu
usage and physical memory usage.</p>

<pre>Returns array wtih 2 element, first element is cpu usage, 2nd is memory usage in kb</pre>
            

            
            <div class="method-source-code" id="get_latest_for_server-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 27</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_latest_for_server</span>
  <span class="ruby-identifier">t1</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
  <span class="ruby-identifier">interval</span> = <span class="ruby-value">60</span> <span class="ruby-comment"># in seconds</span>
  <span class="ruby-identifier">t2</span> = <span class="ruby-identifier">t1</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">interval</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-identifier">select</span>(<span class="ruby-string">'sum(cpu_usage) as tot_cpu,
                   sum(memory_usage) as tot_memory'</span>
                   ).<span class="ruby-identifier">where</span>(<span class="ruby-string">'wall_time &lt;= ? and
                   wall_time &gt;= ?'</span>,
                   <span class="ruby-identifier">t1</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>),<span class="ruby-identifier">t2</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)).<span class="ruby-identifier">first</span>
  <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">result</span>
    <span class="ruby-identifier">res</span> = [<span class="ruby-value">0.0</span>, <span class="ruby-value">0.0</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">res</span> = [<span class="ruby-identifier">result</span>.<span class="ruby-identifier">tot_cpu</span>.<span class="ruby-identifier">to_f</span>, <span class="ruby-identifier">result</span>.<span class="ruby-identifier">tot_memory</span>.<span class="ruby-identifier">to_i</span>]
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">res</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_latest_for_server-source -->
            
          </div>

          

          
        </div><!-- get_latest_for_server-method -->

      
        <div id="get_max_and_slab-method" class="method-detail ">
          <a name="method-c-get_max_and_slab"></a>

          
          <div class="method-heading">
            <span class="method-name">get_max_and_slab</span><span
              class="method-args">(max)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>This method gives the maximum value for y axis and the value by which the y
axis is to be partitioned.</p>
            

            
            <div class="method-source-code" id="get_max_and_slab-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 176</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_max_and_slab</span>(<span class="ruby-identifier">max</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
  <span class="ruby-identifier">max</span> = <span class="ruby-value">1</span>
  <span class="ruby-identifier">slab</span> = <span class="ruby-value">1</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">8</span>
    <span class="ruby-identifier">slab</span> = <span class="ruby-identifier">max</span><span class="ruby-operator">/</span><span class="ruby-value">8</span>.<span class="ruby-identifier">to_i</span>
    <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">slab</span> = <span class="ruby-value">1</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">max</span> = <span class="ruby-identifier">max</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">slab</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">max</span>, <span class="ruby-identifier">slab</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_max_and_slab-source -->
            
          </div>

          

          
        </div><!-- get_max_and_slab-method -->

      
        <div id="get_server_resource_usage-method" class="method-detail ">
          <a name="method-c-get_server_resource_usage"></a>

          
          <div class="method-heading">
            <span class="method-name">get_server_resource_usage</span><span
              class="method-args">(start_time, end_time, type)</span>
            <span class="method-click-advice">click to toggle source</span>
          </div>
          

          <div class="method-description">
            
            <p>This method Returns the Resource Usage data for the server. This data is
used to plot the graph for cpu usage and memory usage of the server. CPU
usage is in percentage and the physical memory usage is MBs.</p>
            

            
            <div class="method-source-code" id="get_server_resource_usage-source">
<pre>
<span class="ruby-comment"># File app/models/resource_usage.rb, line 91</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">get_server_resource_usage</span>(<span class="ruby-identifier">start_time</span>, <span class="ruby-identifier">end_time</span>, <span class="ruby-identifier">type</span>)
  <span class="ruby-identifier">max</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">interval</span> = <span class="ruby-value">0</span>
  <span class="ruby-identifier">interval</span> = ((<span class="ruby-identifier">end_time</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">start_time</span>) <span class="ruby-operator">/</span> <span class="ruby-value">60</span>).<span class="ruby-identifier">to_i</span>
  <span class="ruby-identifier">final_data</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">interval</span>)
  <span class="ruby-identifier">wall_time</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">interval</span>)
  <span class="ruby-identifier">resource_usages</span> = <span class="ruby-identifier">select</span>(<span class="ruby-string">'wall_time,
                            sum(cpu_usage) as cpu_usage,
                            sum(memory_usage) as memory_usage'</span>
                            ).<span class="ruby-identifier">where</span>(<span class="ruby-string">'wall_time &gt;= ? and wall_time &lt; ?'</span>,
                            <span class="ruby-identifier">start_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>), <span class="ruby-identifier">end_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)
                            ).<span class="ruby-identifier">group</span>(<span class="ruby-value">:wall_time</span>)

  <span class="ruby-identifier">resource_usages</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">resource_usage</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">current_time</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">local</span>(<span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">year</span>,  <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">month</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">day</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">hour</span>, <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">wall_time</span>.<span class="ruby-identifier">min</span>, <span class="ruby-string">'0'</span>)
    <span class="ruby-identifier">index</span> = (<span class="ruby-identifier">current_time</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">start_time</span>) <span class="ruby-operator">/</span> <span class="ruby-value">60</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;CPU&quot;</span>
      <span class="ruby-identifier">total_data</span> = <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">cpu_usage</span>
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;Memory&quot;</span>
      <span class="ruby-identifier">total_data</span> = <span class="ruby-identifier">resource_usage</span>.<span class="ruby-identifier">memory_usage</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">1024</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">total_data</span>
      <span class="ruby-identifier">max</span> = <span class="ruby-identifier">total_data</span>.<span class="ruby-identifier">to_i</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">final_data</span>[<span class="ruby-identifier">index</span>] = <span class="ruby-identifier">total_data</span>
    <span class="ruby-identifier">wall_time</span>[<span class="ruby-identifier">index</span>] = <span class="ruby-identifier">current_time</span>.<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%H:%M&quot;</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">interval</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">final_data</span>[<span class="ruby-identifier">i</span>].<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">wall_time</span>[<span class="ruby-identifier">i</span>] = (<span class="ruby-identifier">start_time</span><span class="ruby-operator">+</span><span class="ruby-identifier">i</span>*<span class="ruby-value">60</span>).<span class="ruby-identifier">strftime</span>(<span class="ruby-string">&quot;%H:%M&quot;</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">max</span>, <span class="ruby-identifier">slab</span> = <span class="ruby-identifier">get_max_and_slab</span>(<span class="ruby-identifier">max</span>)
  <span class="ruby-identifier">fill_gaps!</span>(<span class="ruby-identifier">final_data</span>)
  <span class="ruby-identifier">step</span> = (<span class="ruby-identifier">interval</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>) <span class="ruby-operator">/</span> <span class="ruby-value">20</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">wall_time</span>, <span class="ruby-identifier">final_data</span>, <span class="ruby-identifier">max</span>, <span class="ruby-identifier">slab</span>, <span class="ruby-identifier">step</span>
<span class="ruby-keyword">end</span></pre>
            </div><!-- get_server_resource_usage-source -->
            
          </div>

          

          
        </div><!-- get_server_resource_usage-method -->

      
      </div><!-- public-class-method-details -->
    
    </div><!-- 5Buntitled-5D -->
  

  </div><!-- documentation -->

  <div id="validator-badges">
    <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
    <p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
      Rdoc Generator</a> 2</small>.</p>
  </div>

</body>
</html>

